﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.LuFang.API.Application.Command.BreedCommand;
using RBAC.LuFang.API.Application.Command.EnterpriseCommand;
using RBAC.LuFang.API.Application.Command.GoodsCommand;
using RBAC.LuFang.API.Application.Command.heathCommand;
using RBAC.LuFang.API.Application.Command.imgCommand;
using RBAC.LuFang.API.Dtos;
using RBAC.LuFang.Domain.Traceability;
using RBAC.LuFang.Domin.Services.TraceabilityService;
using System.Collections.Generic;

namespace RBAC.LuFang.API.Controllers
{
    /// <summary>
    /// 商品控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class GoodsController : ControllerBase
    {
        private readonly IMediator _mediator;

        public GoodsController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<string>> Files(IFormFile file)
        {
            ApiResult<string> result = new ApiResult<string>();
            try
            {
                string a = Directory.GetCurrentDirectory() + @"\wwwroot\pic\";
                string b = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                using (FileStream fs=new FileStream(a+b,FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                result.Data = b;
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Goodspage>> GetGoods([FromQuery]GoodsSelectCommand goods)
        {
            ApiResult<Goodspage> result=new ApiResult<Goodspage>();
            try
            {
                result.Data = await _mediator.Send(goods);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddGoods(GoodsCreateCommand goods)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(goods);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> PiDelGoods(GoodsDeleteCommand ids)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
               result.Data=await _mediator.Send(ids);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 商品反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<Goods>> GetGoods(GoodsFanCommand ids)
        {
            ApiResult<Goods> result = new ApiResult<Goods>();
            try
            {
                result.Data = await _mediator.Send(ids);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 商品修改
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdGoods(GoodsUpdCommand goods)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(goods);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="imges"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPic(imgAddChanPinCommand imges)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(imges);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示产品检验报告图片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<imges>>> GetChanPinImg(imgSelectChanPinCommand imgtype)
        {
            ApiResult<List<imges>> result = new ApiResult<List<imges>>();
            try
            {
                result.Data = await _mediator.Send(imgtype);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除产品检验报告图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelChanPin(imgDelCommand id)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(id);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }






        ///////////////
        ///////////////
        //////////////
        /////////////养殖

        /// <summary>
        /// 养殖添加
        /// </summary>
        /// <param name="breed"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddBreed(BreedCreateCommand breed)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(breed);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据商品信息显示不同的养殖信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<BreedInfo>>> GetBreed(BreedSelectCommand goodsid)
        {
            ApiResult<List<BreedInfo>> result = new ApiResult<List<BreedInfo>>();
            try
            {
                result.Data = await _mediator.Send(goodsid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填养殖信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<BreedInfo>> FanBreed(BreedFanCommand goodsid)
        {
            ApiResult<BreedInfo> result = new ApiResult<BreedInfo>();
            try
            {
                result.Data = await _mediator.Send(goodsid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 显示保健列表
        /// </summary>
        /// <param name="breedid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<health>>> Gethealth(heathSelectCommand goodsId)
        {
            ApiResult<List<health>> result = new ApiResult<List<health>>();
            try
            {
                result.Data = await _mediator.Send(goodsId);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="healthid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delhealth(heathDeleteCommand healthid)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(healthid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改保健
        /// </summary>
        /// <param name="health"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdHeath(UpdHeathCommand health)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(health);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="health"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Addhealth(heathCreateCommand health)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(health);
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示动检证照片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<imges>>> GetHeathImg(imgHeathSelectCommand img)
        {
            ApiResult<List<imges>> result= new ApiResult<List<imges>>();
            try
            {
                result.Data = await _mediator.Send(img);
            }
            catch (Exception ee)
            {
                result.Code=-1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除动检证照片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelHeath(imgDelHeathCommand id)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(id);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 更新企业信息
        /// </summary>
        /// <param name="enterprise"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> AddEnterprise(EnterpriseUpdCommand enterprise)
        {
            ApiResult<int> result = new ApiResult<int>();   
            try
            {
                result.Data=await _mediator.Send(enterprise); 
            }
            catch (Exception ee)
            {
                result.Code= -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填企业信息
        /// </summary>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<Enterprise>> FanEnter(EnterpriseFanCommand goodsId)
        {
            ApiResult<Enterprise> result= new ApiResult<Enterprise>();
            try
            {
                result.Data = await _mediator.Send(goodsId);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示企业信息
        /// </summary>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<Enterprise>>> GetEnter(EnterpriseSelectCommand goodsid)
        {
            ApiResult<List<Enterprise>> result= new ApiResult<List<Enterprise>>();
            try
            {
                result.Data = await _mediator.Send(goodsid);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示养殖下的企业照片
        /// </summary>
        /// <param name="imgtype"></param>
        /// <param name="goodsid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<imges>>> GetEnterPic([FromQuery]imgSelectYEnterCommand imgSelect)
        {
            ApiResult<List<imges>> result= new ApiResult<List<imges>>();
            try
            {
                result.Data = await _mediator.Send(imgSelect);
            }
            catch (Exception e)
            {
                result.Code= -1;
                result.Message = e.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除养殖下的企业照片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelEnterPic(imgDelYEnterCommand id)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(id);
            }
            catch (Exception ee)
            {
                result.Code=-1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }













    }
}
